package com.huaxin.device.dto;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 响应码
 *
 * @author
 * @date 2025/04/01
 */
@Data
@NoArgsConstructor // 无参构造函数
@AllArgsConstructor // 全参构造函数
public class ResponseResult<T> {
    private int code;
    private String message;
    private T data;

    // 成功的返回
    public static <T> ResponseResult<T> ok(T data) {
        return new ResponseResult<>(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getMessage(), data);
    }

    // 错误的返回，接受自定义的消息
    public static <T> ResponseResult<T> failure(ResponseCode responseCode, String customMessage, T data) {
        String message = (customMessage != null && !customMessage.trim().isEmpty()) ? customMessage : responseCode.getMessage();
        return new ResponseResult<>(responseCode.getCode(), message, data);
    }

    // 参数错误
    public static <T> ResponseResult<T> parameterError(T data, String customMessage) {
        return failure(ResponseCode.BAD_REQUEST, customMessage, data);
    }

    // 未授权
    public static <T> ResponseResult<T> unauthorized(T data, String customMessage) {
        return failure(ResponseCode.UNAUTHORIZED, customMessage, data);
    }

    // 禁止访问
    public static <T> ResponseResult<T> forbidden(T data, String customMessage) {
        return failure(ResponseCode.FORBIDDEN, customMessage, data);
    }

    // 资源未找到
    public static <T> ResponseResult<T> notFound(T data, String customMessage) {
        return failure(ResponseCode.NOT_FOUND, customMessage, data);
    }

    //验证码错误
    public static <T> ResponseResult<T> verificationCodeError(T data, String customMessage) {
        return failure(ResponseCode.VERIFICATION_CODE_ERROR, customMessage, data);
    }
}